<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class CI_Media_entity{
	var $current_dbprefix = "";
	var $CI;
	var $id = 0;
	var $escort_id = 0;
	var $type = "";
	var $permission = "";
	var $alt_text = "";
	var $desc_text = "";
	var $name = "";
	var $ordering = 0;
	var $clicked_no = 0;
	var $avatar = "";
	var $created_date = 0;
	var $public_top_image_big = "";
	var $public_top_image_thin = "";
	var $public_image = "";
	var $add_point = 0;
	var $review = 0;
	
	function __construct() {
		$this->CI= & get_instance();
		$this->current_dbprefix = $this->CI->db->dbprefix;
		$this->CI->db->set_dbprefix('default_');
	}
	function __destruct() {
		$this->CI->db->set_dbprefix($this->current_dbprefix);
	}
    
	function init($cmd, $input)
	{
		$this->CI->db->set_dbprefix('default_');
		
		if($cmd == 'id')
		{
			$result = $this->CI->db->where('id',$input)->get(TBL_MEDIA)->result();
		}
		else
		{
			return false;
		}
		
		if( !empty($result) )
		{
			$this->id = $result[0]->id;
			$this->escort_id = $result[0]->escort_id;
			$this->type = $result[0]->type;
			$this->permission = $result[0]->permission;
			$this->alt_text = $result[0]->alt_text;
			$this->desc_text = $result[0]->desc_text;
			$this->name = $result[0]->name;
			$this->ordering = $result[0]->ordering;
			$this->clicked_no = $result[0]->clicked_no;
			$this->avatar = $result[0]->avatar;
			$this->created_date = $result[0]->created_date;
			$this->public_top_image_big = $result[0]->public_top_image_big;
			$this->public_top_image_thin = $result[0]->public_top_image_thin;
			$this->public_image = $result[0]->public_image;
			$this->add_point = $result[0]->add_point;
			$this->review = $result[0]->review;
			
			return $this;
		}
		return false;
	}
	
	function setEscortId($input){ $this->escort_id=$input; }
	function setType($input){ $this->type=$input; }
	function setPermission($input){ $this->permission=$input; }
	function setAltText($input){ $this->alt_text=$input; }
	function setDescText($input){ $this->desc_text=$input; }
	function setName($input){ $this->name=$input; }
	function setOrdering($input){ $this->ordering=$input; }
	function setClickedNo($input){ $this->clicked_no=$input; }
	function setAvatar($input){ $this->avatar=$input; }
	function setCreatedDate($input){ $this->created_date=$input; }
	function setPublicTopImageBig($input){ $this->public_top_image_big=$input;}
	function setPublicTopImageThin($input){ $this->public_top_image_thin=$input;}
	function setPublicImage($input){ $this->public_image=$input;}
	function setAddPoint($input){ $this->add_point=$input;}
	function setReview($input){ $this->review=$input; }
	
	function getId(){ return $this->id; }
	function getEscortId(){return $this->escort_id; }
	function getType(){return $this->type; }
	function getPermission(){return $this->permission; }
	function getAltText(){return $this->alt_text; }
	function getDescText(){return $this->desc_text; }
	function getName(){return $this->name; }
	function getOrdering(){return $this->ordering; }
	function getClickedNo(){return $this->clicked_no; }
	function getAvatar(){return $this->avatar; }
	function getCreatedDate(){return $this->created_date; }
	function getPublicTopImageBig(){ return $this->public_top_image_big;}
	function getPublicTopImageThin(){ return $this->public_top_image_thin;}
	function getPublicImage(){ return $this->public_image;}
	function getAddPoint(){ return $this->add_point;}
	function getReview(){ return $this->review; }
	
	function update()
	{
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('escort_id', $this->escort_id);
		$this->CI->db->set('type', $this->type);
		$this->CI->db->set('permission', $this->permission);
		$this->CI->db->set('alt_text', $this->alt_text);
		$this->CI->db->set('desc_text', $this->desc_text);
		$this->CI->db->set('name', $this->name);
		$this->CI->db->set('ordering', $this->ordering);
		$this->CI->db->set('clicked_no', $this->clicked_no);
		$this->CI->db->set('avatar', $this->avatar);
		$this->CI->db->set('public_top_image_big', $this->public_top_image_big);
		$this->CI->db->set('public_top_image_thin', $this->public_top_image_thin);
		$this->CI->db->set('public_image', $this->public_image);
		$this->CI->db->set('add_point', $this->add_point);
		$this->CI->db->set('review', $this->review);
		
		$this->CI->db->where('id', $this->id)->update(TBL_MEDIA);
	}
	
	function createNewMediaEntity(){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->set('created_date', time() );
		$this->CI->db->insert(TBL_MEDIA);
		
		return $this->CI->db->insert_id();
	}
	 
	function getCurrentOrdering($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$result = $this->CI->db->where('escort_id',$escort_id)->get(TBL_MEDIA)->result();
		return intval(count($result));
	}
	
	function getMediaByPermission($escort_id,$permission='private'){
		//permission:private, public, public_top
		$this->CI->db->set_dbprefix('default_');
		
		$result = $this->CI->db->where(array('escort_id'=>$escort_id, 'permission'=>$permission))->order_by('ordering','desc')->get(TBL_MEDIA)->result();
		return $result;
	}
	
	function getTargetFileImageTopPublic($type = 'big'){ //big | thin
		$ESCORT = $this->CI->escort_entity->init('id',$this->getEscortId());
		if($type == 'big')
			return site_url()."uploads/default/users/".$ESCORT->getUserId()."/".$this->getPublicTopImageBig();
		else
			return site_url()."uploads/default/users/".$ESCORT->getUserId()."/".$this->getPublicTopImageThin();
	}
	
	function getTargetFileImagePublic(){
		$ESCORT = $this->CI->escort_entity->init('id',$this->getEscortId());
		return site_url()."uploads/default/users/".$ESCORT->getUserId()."/".$this->getPublicImage();
	}
	
	function getTargetFile($type='image'){
		//type: image, video, avatar
		$ESCORT = $this->CI->escort_entity->init('id',$this->getEscortId());
		if($type== 'avatar'){
			if(! $this->getAvatar()){
				return site_url()."config/video_icon_default.jpg";
			}
			return site_url()."uploads/default/users/".$ESCORT->getUserId()."/".$this->getAvatar();
		}else{
			return site_url()."uploads/default/users/".$ESCORT->getUserId()."/".$this->getName();
		}	
	}
	function getFilePath($type='image'){
		//type: image, video, avatar
		$ESCORT = $this->CI->escort_entity->init('id',$this->getEscortId());
		if($type== 'avatar'){
			if(! $this->getAvatar()){
				return "config/video_icon.jpg";
			}
			return "./uploads/default/users/".$ESCORT->getUserId()."/".$this->getAvatar();
		}else{
			return "./uploads/default/users/".$ESCORT->getUserId()."/".$this->getName();
		}
	}
	
	function getTypeOfPublicTopMedia($escort_id){ // image|video|empty string
		$objectResult = $this->getMediaByPermission($escort_id,$permission='public_top');
		if(! count($objectResult)){
			return '';
		}
		return $objectResult[0]->type ;
	}
	
	function getUserAvatarPicture($escort_id){
		//$type_public_top = $this->getTypeOfPublicTopMedia($escort_id);
		$ESCORT = $this->CI->escort_entity->init('id',$escort_id);
	 
	//	if( $type_public_top == 'image' ){
	//		$obj = $this->getMediaByPermission($escort_id,$permission='public_top');
	//		return site_url()."uploads/default/users/".$ESCORT->getUserId()."/".$obj[0]->public_image;
	//	}else{
			$obj = $this->getMediaByPermission($escort_id,$permission='public');
			foreach($obj as $item){
				if($item->type == 'image'){
					return site_url()."uploads/default/users/".$ESCORT->getUserId()."/".$item->public_image;
				}
			}
			return site_url()."config/escorts_default.png";
	//	}
	}
	
	function findUserAvatarMediaId($escort_id){
		$obj = $this->getMediaByPermission($escort_id,$permission='public');
		foreach($obj as $item){
			if($item->type == 'image'){
				return $item->id;
			}
		}
		return 0;
	}
	
	function getAltAndTitleText($media_id, $location=''){
		if($media_id == 0) return '';
		
		$MEDIA = $this->init('id',$media_id);
		$ESCORT = $this->CI->escort_entity->init('id',$MEDIA->escort_id);
		if(! $locationString= $location){
			$locationString = getLocationZoneOfEscort($ESCORT->id);
		}
		$escortname = $ESCORT->getEscortName();
		
		$alttext = str_replace(array('$1','$2','$3','$4'), array($escortname,$locationString,$MEDIA->ordering,$MEDIA->alt_text) , language_translate('picture_alt_label'));
		$titletext = str_replace(array('$1','$2','$3','$4'), array($escortname,$locationString,$MEDIA->ordering,$MEDIA->desc_text) , language_translate('picture_desc_label'));
		
		return array('alt'=>$alttext,'title'=>$titletext);
	}
	
	function _getAltText($media_id, $location=''){
		if($media_id == 0) return '';
		
		$MEDIA = $this->init('id',$media_id);
		$ESCORT = $this->CI->escort_entity->init('id',$MEDIA->escort_id);
		if(! $locationString= $location){
			$locationString = getLocationZoneOfEscort($ESCORT->id);
		}
		$escortname = $ESCORT->getEscortName();
		
		$text = str_replace(array('$1','$2','$3','$4'), array($escortname,$locationString,$MEDIA->ordering,$MEDIA->alt_text) , language_translate('picture_alt_label'));
		return $text;
	}
	
	function _getDescText($media_id, $location=''){
		if($media_id == 0) return '';
		
		$MEDIA = $this->init('id',$media_id);
		$ESCORT = $this->CI->escort_entity->init('id',$MEDIA->escort_id);
		$locationString = getLocationZoneOfEscort($ESCORT->id);
		$escortname = $ESCORT->getEscortName();
		
		$text = str_replace(array('$1','$2','$3','$4'), array($escortname,$locationString,$MEDIA->ordering,$MEDIA->desc_text) , language_translate('picture_desc_label'));
		return $text;
	}
	
	function delete($id){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->where('id',$id);
		return $this->CI->db->delete(TBL_MEDIA);
	}
	function getAllMedia($limit){
		$this->CI->db->set_dbprefix('default_');
		
		$result = $this->CI->db->limit($limit[0], $limit[1])->order_by('created_date','desc')->get(TBL_MEDIA)->result();
		return $result;
	}
	function countAllMedia(){
		$this->CI->db->set_dbprefix('default_');
		
		return $this->CI->db->order_by('created_date','desc')->count_all_results(TBL_MEDIA);
	}
	function getObject($mediaInfo){
		$this->id = $mediaInfo->id;
		$this->escort_id = $mediaInfo->escort_id;
		$this->type = $mediaInfo->type;
		$this->permission = $mediaInfo->permission;
		$this->alt_text = $mediaInfo->alt_text;
		$this->desc_text = $mediaInfo->desc_text;
		$this->name = $mediaInfo->name;
		$this->ordering = $mediaInfo->ordering;
		$this->clicked_no = $mediaInfo->clicked_no;
		$this->avatar = $mediaInfo->avatar;
		$this->created_date = $mediaInfo->created_date;
		$this->public_top_image_big = $mediaInfo->public_top_image_big;
		$this->public_top_image_thin = $mediaInfo->public_top_image_thin;
		$this->public_image = $mediaInfo->public_image;
		$this->review = $mediaInfo->review;
		return $this;
	}
	
	function getObjectOfMostPopularImage($escort_id){
		$this->CI->db->set_dbprefix('default_');
		
		$rs = $this->CI->db->where(array('type'=>'image','escort_id'=>$escort_id))->order_by('clicked_no','desc')->get(TBL_MEDIA)->result();
		return ($rs) ? $this->init('id',$rs[0]->id) : false;
	}
	
	function getMediaListByEscortIdList($escortIdList, $limit, $review = ''){
		$this->CI->db->set_dbprefix('default_');
		
		if($review != ''){
			$this->CI->db->where('review', $review);
		}
		$result = $this->CI->db->where_in('escort_id', $escortIdList)->limit($limit[0], $limit[1])->order_by('escort_id','desc')->order_by('created_date','desc')->get(TBL_MEDIA)->result();
		return $result;
	}
	function countMediaListByEscortIdList($escortIdList, $review = ''){
		$this->CI->db->set_dbprefix('default_');
		
		$this->CI->db->where_in('escort_id', $escortIdList);
		if($review != ''){
			$this->CI->db->where('review', $review);
		}
		return $this->CI->db->count_all_results(TBL_MEDIA);
	}	
	//end class
}
